#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Agresso.Interface.Layer.Domain;
using Agresso.Interface.Layer.Service;
using System.Collections;
using System.IO;
using System.Configuration;
using System.Reflection;

#endregion

namespace Agresso.Interface.Layer.Web
{
    public partial class UserControlWinSDSPurchaseOrderView : UserControl
    {
        User m_current_user;
        MainForm m_mainForm;
        WinSDSService m_winSDSService;
        string m_startDefault;
        Timer m_timerSearch = new Timer();


        public UserControlWinSDSPurchaseOrderView(string name, User currentUser, MainForm mainForm)
        {
            InitializeComponent();
            labelTitle.Text = name;
            this.Name = name;
            m_current_user = currentUser;
            m_mainForm = mainForm;
            m_startDefault = ConfigurationManager.AppSettings["StartPath"];
            m_winSDSService = new WinSDSService();

            toolBarButtonRefresh.Click += new EventHandler(toolBarButtonRefresh_Click);
            toolBarButtonExcel.Click += new EventHandler(toolBarButtonExcel_Click);
            listViewPO.DoubleClick += new EventHandler(listViewAlert_DoubleClick);
          //  toolBarButtonRefresh_Click(null, null);

            listViewPO.Columns.Clear();
            FieldInfo[] column = typeof(jPOHeader).GetFields();
            foreach (FieldInfo pi in column)
            {
                listViewPO.Columns.Add(pi.Name, 100, HorizontalAlignment.Left);
            }

            ListViewTools.LoadConfiguration(listViewPO, m_current_user);
            listViewPO.ColumnWidthChanged += new ColumnWidthChangedEventHandler(listViewPO_ColumnWidthChanged);
            
            initTimerSearch();
        }

        void listViewPO_ColumnWidthChanged(object sender, ColumnWidthChangedEventArgs e)
        {
            ListViewTools.SaveConfiguration(listViewPO, m_current_user);
        }

        private void initTimerSearch()
        {
            m_timerSearch.Interval = 100;
            m_timerSearch.Tick += new EventHandler(m_timerSearch_Tick);
        }

        void m_timerSearch_Tick(object sender, EventArgs e)
        {
            Search();
            m_timerSearch.Stop();
        }

        private void Search()
        {
            listViewPO.Items.Clear();
            statusBar1.Text = "Total : 0 item(s)";
            IList softmarAlert = m_winSDSService.RetrieveAllPOData(textBoxSearch.Text.Trim(), checkBoxPickupFlag.Checked);
            LoadItems(softmarAlert);
           
        }

        void listViewAlert_DoubleClick(object sender, EventArgs e)
        {
            if (listViewPO.SelectedItem == null) return;
            if (listViewPO.SelectedItem.Tag == null) return;
            jPOHeader a = (jPOHeader)listViewPO.SelectedItem.Tag;
            WinSDSPODetailsForm adf = new WinSDSPODetailsForm(a, m_current_user);
            adf.ShowDialog();
        }

        void toolBarButtonExcel_Click(object sender, EventArgs e)
        {
            if (listViewPO.Items.Count == 0)
            {
                MessageBox.Show("No Data to export, please refresh first.");
                return;
            }
            DataSet ds = new DataSet();
            DataTable dt = ds.Tables.Add("Table");
            foreach (ColumnHeader column in listViewPO.Columns)
            {
                dt.Columns.Add(column.Text);
            }
            foreach (ListViewItem itm in listViewPO.Items)
            {
                ArrayList row = new ArrayList();
                foreach (ColumnHeader column in listViewPO.Columns)
                {
                    row.Add(itm.SubItems[column.Index].Text);
                }
                dt.Rows.Add(row.ToArray());
            }

            System.Web.UI.WebControls.DataGrid grid =
                  new System.Web.UI.WebControls.DataGrid();
            grid.HeaderStyle.Font.Bold = true;
            grid.HeaderStyle.Font.Name = "Calibri";
            grid.HeaderStyle.BackColor = Color.Gold;
            grid.Font.Name = "Calibri";
            grid.Font.Size = 9;
            grid.DataSource = ds;
            grid.DataMember = "Table";
            grid.DataBind();
            string filename = "reportSoftmar" + DateTime.Now.Second.ToString() + ".xls";
            string filePath = m_startDefault + filename;
            using (StreamWriter sw = new StreamWriter(filePath))
            {
                using (System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw))
                {
                    grid.RenderControl(hw);
                }
            }
            Link.Open(filename);
        }

        void toolBarButtonRefresh_Click(object sender, EventArgs e)
        {
            listViewPO.Items.Clear();
            statusBar1.Text = "Total : 0 item(s)";
            IList softmarAlert = m_winSDSService.RetrieveAllPOData();
           LoadItems(softmarAlert);
        }

        private void LoadItems(IList softmarAlert)
        {
            foreach (jPOHeader a in softmarAlert)
            {
                ListViewItem item = new ListViewItem();
                foreach (FieldInfo fi in typeof(jPOHeader).GetFields())
                {
                    item.SubItems.Add(fi.GetValue(a) == null ? "" : fi.GetValue(a).ToString());
                }
                listViewPO.Items.Add(item); // alert date..
                //item.SubItems.Add(a.entity);
                //item.SubItems.Add(a.alert_description);
                //item.SubItems.Add(a.alert_status.ToString());
                //item.SubItems.Add(a.alert_key);
                item.Tag = a;
            }
            statusBar1.Text = string.Format("Total : {0} item(s)", softmarAlert.Count);
            this.listViewPO.Font = new System.Drawing.Font("Tahoma", 6.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            listViewPO.Update();
        }

        private void textBoxSearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(e.KeyChar == Convert.ToChar(Keys.Enter))
            {
                m_timerSearch.Start();
            }
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            Search();
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            ListViewColumnForm p = new ListViewColumnForm(listViewPO, m_current_user);
            p.ShowDialog();
        }
    }
}